//
//  YWAnimationView.m
//  maintain
//
//  Created by 郑想 on 15/12/17.
//  Copyright © 2015年 ewin. All rights reserved.
//

#import "UIColor+CustomColors.h"
#import "YWAnimationView.h"
#import "YWMacro.h"

typedef void (^YWAnimationCompletionBlock)();

@interface YWAnimationView () {
    YWAnimationCompletionBlock completeBlock;
    NSTimeInterval _duration;
}
@end

@implementation YWAnimationView

+ (instancetype)animationView
{
    return [[self alloc] init];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        _type = YWAnimationViewTypeRound;
    }
    return self;
}

- (void)setType:(YWAnimationViewType)type
{
    _type = type;
    [self setShowStatus:_showStatus];
}

- (void)setShowStatus:(YWAnimationViewShowStatus)showStatus
{
    _showStatus = showStatus;
    switch (showStatus) {
    case YWAnimationViewShowStatusUnfinish:
        self.image = [UIImage imageNamed:(_type == YWAnimationViewTypeRound ? @"unfinish_expire" : @"unfinish_expire_rect")];//@"unfinish" : @"unfinish_rect"
        break;
    case YWAnimationViewShowStatusPending:
        self.image = [UIImage imageNamed:(_type == YWAnimationViewTypeRound ?@"unfinish_expire" : @"unfinish_expire_rect")];//@"pended":@"pended_rect"
        break;
    case YWAnimationViewShowStatusExpire:
        self.image = [UIImage imageNamed:(_type == YWAnimationViewTypeRound ? @"unfinish_expire" : @"unfinish_expire_rect")];
        break;
    case YWAnimationViewShowStatusExpired:
        self.image = [UIImage imageNamed:(_type == YWAnimationViewTypeRound ? @"unfinish_expire" : @"unfinish_expire_rect")];
        break;
    case YWAnimationViewShowStatusFinished:
        self.image = [UIImage imageNamed:(_type == YWAnimationViewTypeRound ? @"finished" : @"finished_rect")];
        break;
    default:
        break;
    }
}

- (void)startAnimationWithDuration:(NSTimeInterval)duration repeatCount:(NSUInteger)repeatCount completion:(void (^)())completion
{
    completeBlock = completion;
    _duration = duration;
    [UIView beginAnimations:@"YWWorkListCellAnimationLabel" context:NULL];
    [UIView setAnimationDuration:duration]; //设置动画的持续时间
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationDidStop)];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear]; //动画的曲线
    [UIView setAnimationRepeatCount:repeatCount]; //动画的重复次数
    [UIView setAnimationRepeatAutoreverses:YES];
    self.transform = CGAffineTransformMakeScale(1.2, 1.2);
    [UIView commitAnimations];
}

- (void)animationDidStop
{
    [UIView animateWithDuration:_duration
                     animations:^{
                         self.transform = CGAffineTransformMakeScale(1, 1);
                         if (completeBlock) {
                             completeBlock();
                         }
                     }];
}
@end
